package com.nuc.thread.synchronizedTest;

import lombok.SneakyThrows;

/**
 * @Description synchronized 对共享的对象加锁
 * @Author 诗酒趁年华
 * @Version V1.0.0
 * @Date 2021/5/9
 */
public class Test2 {
    static  int n = 0;
    static Object lock = new Object();

    @SneakyThrows
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            synchronized (lock){
                for (int i = 0; i < 500; i++) {
                    n = n+1;
                }
            }

        },"t1");

        Thread t2 = new Thread(()->{
            synchronized (lock){
                for (int i = 0; i < 500; i++) {
                    n = n-1;
                }
            }

        },"t2");

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("n的值"+n);

    }

    public synchronized static void test1(){

    }

    public  static  void test2(){
        synchronized (Test2.class){

        }
    }

}


